package org.feng.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.feng.entity.PubFile;
import org.feng.entity.result.UploadResult;
import org.feng.service.IPubFileService;
import org.feng.utils.UploadFileUtils;
import org.feng.base.entity.Result;
import org.feng.support.UploadSupport;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import jakarta.annotation.Resource;

import java.io.IOException;
import java.time.LocalDateTime;

/**
 * 上传文件
 *
 * @author : FengShuai
 * @since : 2024/6/27 9:24
 */
@RestController
@RequestMapping("/upload")
@Tag(name = "上传")
public class SysUploadController {

    @Resource
    private UploadSupport uploadSupport;
    @Resource
    private IPubFileService sysFileService;

    @PostMapping("/file")
    @Operation(summary = "上传文件")
    public Result<UploadResult> uploadFile(@RequestParam MultipartFile file) throws IOException {
//        String fileMd5 = UploadFileUtils.getFileMd5(file.getInputStream());
//        PubFile dataFile = sysFileService.getOne(Wrappers.<PubFile>lambdaQuery().eq(PubFile::getMd5, fileMd5));
//        if (dataFile != null) {
//            UploadResult uploadResult = new UploadResult();
//            uploadResult.setFileUrl(dataFile.getFilePath());
//            uploadResult.setFileName(dataFile.getFileName());
//            uploadResult.setFileSize(dataFile.getFileSize());
//            uploadResult.setFileType(dataFile.getFileType());
//            return Result.success(uploadResult);
//        }
//
//        UploadResult uploadResult = uploadSupport.build().putFile(file);
//        PubFile pubFile = new PubFile();
//        pubFile.setFileName(uploadResult.getFileName());
//        pubFile.setFileSize(uploadResult.getFileSize());
//        pubFile.setFileType(uploadResult.getFileType());
//        pubFile.setFilePath(uploadResult.getFileUrl());
//        pubFile.setFilePath(uploadResult.getFileUrl());
//        pubFile.setUploadTime(LocalDateTime.now());
//        pubFile.setMd5(fileMd5);
//        pubFile.insert();
        UploadResult uploadResult = uploadSupport.build().putFile(file);
        return Result.success(uploadResult);
    }
}
